package com.work.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.work.biz.domain.BizStudent;
import com.work.biz.domain.BizWorkDetail;
import com.work.biz.mapper.BizStudentMapper;
import com.work.biz.mapper.BizWorkDetailMapper;
import com.work.common.utils.DateUtils;
import com.work.sys.domain.SysUser;
import com.work.sys.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.work.biz.mapper.BizWorkMapper;
import com.work.biz.domain.BizWork;
import com.work.biz.service.IBizWorkService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 作业Service业务层处理
 *
 * @author lee
 * @date 2021-04-22
 */
@Service
public class BizWorkServiceImpl implements IBizWorkService {
    @Autowired
    private BizWorkMapper bizWorkMapper;
    @Autowired
    private BizWorkDetailMapper bizWorkDetailMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private BizStudentMapper studentMapper;

    /**
     * 查询作业
     *
     * @param workId 作业ID
     * @return 作业
     */
    @Override
    public List<BizWorkDetail> selectBizWorkDetailById(Long workId) {
        BizWorkDetail bizWorkDetail = new BizWorkDetail();
        bizWorkDetail.setWorkId(workId);
        return bizWorkDetailMapper.selectBizWorkDetail(bizWorkDetail);
    }

    /**
     * 查询作业
     *
     * @param workId 作业ID
     * @return 作业
     */
    @Override
    public BizWork selectBizWorkById(Long workId) {
        return bizWorkMapper.selectBizWorkById(workId);
    }

    /**
     * 查询作业列表
     *
     * @param bizWork 作业
     * @return 作业
     */
    @Override
    public List<BizWork> selectBizWorkList(BizWork bizWork) {
        return bizWorkMapper.selectBizWorkList(bizWork);
    }

    /**
     * 新增作业
     *
     * @param bizWork 作业
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBizWork(BizWork bizWork) {
        bizWork.setCreateTime(DateUtils.getNowDate());
        SysUser user = userMapper.selectSysUserById(bizWork.getCreatorId());
        bizWork.setCreatorName(user.getNickName());
        int rows = bizWorkMapper.insertBizWork(bizWork);
        List<BizStudent> students = studentMapper.selectBizStudentByClazz(bizWork.getClazzIds());
        students.forEach(s -> {
            BizWorkDetail detail = new BizWorkDetail();
            detail.setStudentId(s.getStudentId());
            detail.setWorkId(bizWork.getWorkId());
            detail.setCreateBy(bizWork.getCreatorName());
            detail.setCreateTime(bizWork.getCreateTime());
            bizWorkDetailMapper.insertBizWorkDetail(detail);
        });
        return rows;
    }

    /**
     * 修改作业
     *
     * @param bizWork 作业
     * @return 结果
     */
    @Override
    public int updateBizWork(BizWork bizWork) {
        bizWork.setUpdateTime(DateUtils.getNowDate());
        return bizWorkMapper.updateBizWork(bizWork);
    }

    /**
     * 批量删除作业
     *
     * @param workIds 需要删除的作业ID
     * @return 结果
     */
    @Override
    public int deleteBizWorkByIds(Long[] workIds) {
        return bizWorkMapper.deleteBizWorkByIds(workIds);
    }

    /**
     * 删除作业信息
     *
     * @param workId 作业ID
     * @return 结果
     */
    @Override
    public int deleteBizWorkById(Long workId) {
        return bizWorkMapper.deleteBizWorkById(workId);
    }
}
